#!/bin/bash

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

. config/options $1

if [ -z "$1" ]; then
  echo "usage: $0 package_name"
  exit 1
fi

if [ -n "$PKG_ARCH" -a ! "$PKG_ARCH" = "any" ]; then
  echo "$PKG_ARCH" | grep -q "$TARGET_ARCH" || exit 0
  echo "$PKG_ARCH" | grep -q "\-$TARGET_ARCH" && exit 0
fi

if [ -n "$PKG_ADDON_PROJECTS" -a ! "$PKG_ADDON_PROJECTS" = "any" ]; then
  for _PROJECT in $PKG_ADDON_PROJECTS ; do
    if [ "$_PROJECT" = "$PROJECT" ] ; then
      PROJECT_SUPPORTED="yes"
    fi
  done
  if [ ! "$PROJECT_SUPPORTED" = "yes" ] ; then
    echo "*** ERROR: $PKG_ADDON_ID: '$PROJECT' not supported ***"
    exit 0
  fi
fi

if [ -n "$PKG_ADDON_REPOVERSION" -a ! "$PKG_ADDON_REPOVERSION" = "$ADDON_VERSION" ]; then
  echo "*** ERROR: $PKG_ADDON_ID: ADDON_VERSION '$ADDON_VERSION' not supported ***"
  exit 0
fi


pack_addon() {
  IFS=" "
  for i in $PKG_ADDON_REQUIRES; do
    REQUIRES_ADDONNAME=`echo $i | cut -f1 -d ":"`
    REQUIRES_ADDONVERSION=`echo $i | cut -f2 -d ":"`
    REQUIRES="$REQUIRES\n    <import addon=\"$REQUIRES_ADDONNAME\" version=\"$REQUIRES_ADDONVERSION\" />"
  done

  unset IFS

  mkdir -p $ADDON_BUILD/$PKG_ADDON_ID

  if [ ! -f $ADDON_BUILD/$PKG_ADDON_ID/addon.xml ]; then
    cp config/addon/${PKG_ADDON_TYPE}.xml $ADDON_BUILD/$PKG_ADDON_ID/addon.xml
    ADDONVER=$ADDON_VERSION.$PKG_REV
  else
    if [ ! $(which xmlstarlet) ]; then
      echo "*** ERROR: $ADDON has addon.xml shipped, you need 'xmlstarlet' ***"
      exit 255
    fi
    ADDONVER=$(xmlstarlet sel -t -v "/addon/@version" $ADDON_BUILD/$PKG_ADDON_ID/addon.xml)
  fi

  if [ -f $PKG_DIR/source/default.py ]; then
    cp -R $PKG_DIR/source/* $ADDON_BUILD/$PKG_ADDON_ID
  fi

  if [ -f $PKG_DIR/icon/icon.png ]; then
    cp $PKG_DIR/icon/icon.png $ADDON_BUILD/$PKG_ADDON_ID
  fi

  if [ -f $PKG_DIR/changelog.txt ]; then
    cp $PKG_DIR/changelog.txt $ADDON_BUILD/$PKG_ADDON_ID
  fi

  PROVIDER_NAME="libreelec.tv"
  if [ ! -z "$PKG_MAINTAINER" ] ; then
    PROVIDER_NAME="$PKG_MAINTAINER"
  fi
  ADDON_NAME="$PKG_NAME"
  if [ ! -z "$PKG_ADDON_NAME" ] ; then
    ADDON_NAME="$PKG_ADDON_NAME"
  fi
  $SED -e "s|@PKG_ADDON_ID@|$PKG_ADDON_ID|g" \
       -e "s|@ADDON_NAME@|$ADDON_NAME|g" \
       -e "s|@ADDON_VERSION@|$ADDON_VERSION.$PKG_REV|g" \
       -e "s|@REQUIRES@|$REQUIRES|g" \
       -e "s|@PKG_SHORTDESC@|$PKG_SHORTDESC|g" \
       -e "s|@OS_VERSION@|$OS_VERSION|g" \
       -e "s|@PKG_LONGDESC@|$PKG_LONGDESC|g" \
       -e "s|@PKG_DISCLAIMER@|$PKG_DISCLAIMER|g" \
       -e "s|@PROVIDER_NAME@|$PROVIDER_NAME|g" \
       -e "s|@PKG_ADDON_PROVIDES@|$PKG_ADDON_PROVIDES|g" \
       -i $ADDON_BUILD/$PKG_ADDON_ID/addon.xml

  if [ "$2" != "-test" ] ; then
    ADDON_INSTALL_DIR="$TARGET/$ADDONS/$ADDON_VERSION/$PROJECT/$TARGET_ARCH/$PKG_ADDON_ID"
    if [ -f $ADDON_INSTALL_DIR/$PKG_ADDON_ID-$ADDONVER.zip ] ; then
      echo "*** WARNING: $PKG_ADDON_ID-$ADDONVER.zip already exists. not overwriting it ***"
      return 0
    fi
    cd $ADDON_BUILD
    echo "*** compressing Addon $PKG_ADDON_ID ... ***"
    zip -rq $PKG_ADDON_ID-$ADDONVER.zip $PKG_ADDON_ID;
    cd - &>/dev/null

    mkdir -p $ADDON_INSTALL_DIR
    cp $ADDON_BUILD/$PKG_ADDON_ID-$ADDONVER.zip $ADDON_INSTALL_DIR
    if [ -f $ADDON_BUILD/$PKG_ADDON_ID/changelog.txt ]; then
      cp $ADDON_BUILD/$PKG_ADDON_ID/changelog.txt $ADDON_INSTALL_DIR/changelog-$ADDONVER.txt
    fi
    if [ -f $ADDON_BUILD/$PKG_ADDON_ID/icon.png ]; then
      cp $ADDON_BUILD/$PKG_ADDON_ID/icon.png $ADDON_INSTALL_DIR/icon.png
    fi
  fi
}

if [ "$PKG_IS_ADDON" = "yes" ] ; then
  $SCRIPTS/build $@

  printf  "%${BUILD_INDENT}c CREATE ADDON  ($PROJECT/$TARGET_ARCH) $1\n" ' '>&$SILENT_OUT
  export BUILD_INDENT=$((${BUILD_INDENT:-1}+$BUILD_INDENT_SIZE))

  rm -rf $ADDON_BUILD

  if [ "$(type -t addon)" = "function" ]; then
    addon
  else
    echo "*** unsupported package format. please convert your package ***"
    exit 1
  fi

  # HACK for packages tat provide multiple addons like screensavers.rsxs
  # addon's addon() in package.mk should take care for exporting
  # MULTI_ADDONS="addon.boo1 addon.boo2 addon.boo3"
  if [ -n "$MULTI_ADDONS" ] ; then
    for _ADDON in $MULTI_ADDONS ; do
      PKG_ADDON_ID=$_ADDON
      pack_addon
    done
  else
    pack_addon
  fi
fi
